﻿using System.Drawing;
using System.Runtime.Versioning;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace zykControl
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
{

    /// <summary>
    /// 表示zykHFGrid控件的一项,是数据网格的最小单位
    /// </summary>
    /// <remarks></remarks>
    [SupportedOSPlatform("windows")]
    public class ZykHFGridCell(ZykHFGridRow theRow, ZykHFGridColumn theColumn, string theText = "")
    {
        public readonly ZykHFGridRow Row = theRow;
        public readonly ZykHFGridColumn Column = theColumn;
        private string mText = theText;

        /// <summary>
        /// 返回本单元格的文本,如果为单据列,始终返回本区的第一行的对应文本
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public string Text
        {
            get
            {
                if (Index > 0 && IsBill == true)
                    return Area[0][Index].mText;
                else
                    return mText;
            }

            set
            {
                mText = value;
            }
        }

        public ZykHFGridArea Area
        {
            get
            {
                return Row.Area;
            }
        }

        public int Index
        {
            get
            {
                return Column.Index;
            }
        }

        public ZykHFGridColumns Columns
        {
            get
            {
                return Row.Columns;
            }
        }

        public int X
        {
            get
            {
                return Column.ColumnLeft;
            }
        }

        public int Y
        {
            get
            {
                if (IsBill == true)
                    return Area.Y;
                else
                    return Row.Y;
            }
        }

        public int Width
        {
            get
            {
                return Column.Width;
            }
        }

        public int Height
        {
            get
            {
                if (IsBill == true)
                    return Area.Height;
                else
                    return Row.Height;
            }
        }

        public bool IsBill
        {
            get
            {
                return Column.IsBill;
            }
        }

        public ZykHFGridCell? GetCellLeft()
        {
            for (int xx = Index - 1; xx >= 0; xx -= 1)
            {
                if (Columns[xx].Visible == false)
                    continue;
                return Row[xx];
            }

            return null;
        }

        public ZykHFGridCell? GetCellRight()
        {
            for (int xx = Index + 1, loopTo = Columns.Count - 1; xx <= loopTo; xx++)
            {
                if (Columns[xx].Visible == false)
                    continue;
                return Row[xx];
            }

            return null;
        }

        public ZykHFGridCell? GetCellUp()
        {
            if (IsTop == true)
            {
                if (Area.Index == 0)
                    return null;
                for (int xx = Area.Index - 1; xx >= 0; xx -= 1)
                {
                    if (Area.Body[xx].Count == 0)
                        continue;
                    var dArea = Area.Body[xx];
                    return dArea[^1][Index];
                }

                return null;
            }
            else
            {
                return Row[Index - 1];
            }
        }

        public ZykHFGridCell? GetCellDown()
        {
            if (IsBottom == true)
            {
                if (Area.Index == 0)
                    return null;
                for (int xx = Area.Index + 1, loopTo = Area.Body.Count - 1; xx <= loopTo; xx++)
                {
                    if (Area.Body[xx].Count == 0)
                        continue;
                    var dArea = Area.Body[xx];
                    return dArea[^1][Index];
                }

                return null;
            }
            else
            {
                return Row[Index + 1];
            }
        }

        /// <summary>
        /// 如果此单元格属于本区当中的第一行,返回True
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool IsTop
        {
            get
            {
                return Row.IsTop;
            }
        }

        /// <summary>
        /// 如果此单元格属于本区当中的最后一行,返回True,注意:如果为单据项必返回True
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool IsBottom
        {
            get
            {
                if (IsBill == true)
                {
                    return true;
                }
                else
                {
                    return Row.IsBottom;
                }
            }
        }

        /// <summary>
        /// 选中本单元格
        /// </summary>
        /// <remarks></remarks>
        public void ToSelected()
        {
            Row.SelectedCellIndex = Index;
        }

        public bool IsSelected
        {
            get
            {
                if (IsBill == true)
                {
                    if (Area.IsSelected == false)
                        return false;
                }
                else if (Row.IsSelected == false)
                    return false;
                return Row.SelectedCellIndex == Index;
            }
        }

        private Color mBackColor = theRow.BackColor;

        public Color BackColor
        {
            get
            {
                return mBackColor;
            }

            set
            {
                mBackColor = value;
            }
        }

        private Color mForeColor = theRow.ForeColor;

        public Color ForeColor
        {
            get
            {
                return mForeColor;
            }

            set
            {
                mForeColor = value;
            }
        }
    }
}